
import datetime
from flask import Flask
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()

def create_app():    
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://root:abc123456@userdb:3306/user'   
    db.init_app(app)
    return app

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80))
    password = db.Column(db.String(200))
    userinfo_id = db.Column(db.Integer, db.ForeignKey('userinfo.id'))
    userinfo = db.relationship('Userinfo',
        backref=db.backref('userinfo'))

    def __init__(self, username, password, userinfo):
        self.username = username
        self.password = password
        self.userinfo = userinfo

    def __repr__(self):
        return '<User %r>' % self.username

class Userinfo(db.Model):
    __tablename__ = 'userinfo'
    __table_args__ = {
        "mysql_charset": "utf8"
    }
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    studentid = db.Column(db.String(10))
    gender = db.Column(db.String(1))

    def __init__(self, name, studentid, gender):
        self.name = name
        self.studentid = studentid
        self.gender = gender

    def __repr__(self):
        return '<User %r>' % self.name

class Dorm(db.Model):
    __tablename__ = 'dorm'
    id = db.Column(db.Integer, primary_key=True)
    dormid = db.Column(db.String(10))

    def __init__(self, dormid):
        self.dormid = dormid

class Unit(db.Model):
    __tablename__ = 'unit'
    id = db.Column(db.Integer, primary_key=True)
    unitid = db.Column(db.String(10))
    dorm_id = db.Column(db.Integer, db.ForeignKey('dorm.id'))
    dorm = db.relationship('Dorm',
        backref=db.backref('dorm'))

    def __init__(self,unitid, dorm):
        self.unitid = unitid
        self.dorm = dorm

class Room(db.Model):
    __tablename__ = 'room'
    id = db.Column(db.Integer, primary_key=True)
    roomid = db.Column(db.String(10))
    unit_id = db.Column(db.Integer, db.ForeignKey('unit.id'))
    unit = db.relationship('Unit',
        backref=db.backref('unit'))
    gender = db.Column(db.String(1))
    totalbed = db.Column(db.Integer)
    rest = db.Column(db.Integer)
    bad = db.Column(db.Integer)

    def __init__(self, roomid, unit, gender, totalbed = 4, rest = 4, bad = 0):
        self.roomid = roomid
        self.unit = unit
        self.gender = gender
        self.totalbed = totalbed
        self.rest = rest
        self.bad = bad


class Order(db.Model):
    __tablename__ = 'orders'
    id = db.Column(db.Integer, primary_key=True)
    userinfo_id = db.Column(db.Integer, db.ForeignKey('userinfo.id'))
    userinfo = db.relationship('Userinfo')
    unit_id = db.Column(db.Integer, db.ForeignKey('unit.id'))
    unit = db.relationship('Unit')
    gender = db.Column(db.String(1))
    num = db.Column(db.Integer)
    status = db.Column(db.Boolean)
    message = db.Column(db.Text)
    time = db.Column(db.DateTime)

    def __init__(self, userinfo, unit, gender, num, message, status):
        self.userinfo = userinfo
        self.unit = unit
        self.gender = gender
        self.num = num
        self.status = status
        self.message = message
        self.time = datetime.datetime.now()

class Orderinfo(db.Model):
    __tablename__ = 'orderinfo'
    id = db.Column(db.Integer, primary_key=True)
    userinfo_id = db.Column(db.Integer, db.ForeignKey('userinfo.id'))
    userinfo = db.relationship('Userinfo')
    order_id = db.Column(db.Integer, db.ForeignKey('orders.id'))
    orders = db.relationship('Order')

    def __init__(self, userinfo, order):
        self.userinfo = userinfo
        self.orders = order


class Information(db.Model):
    __tablename__ = 'information'
    id = db.Column(db.Integer, primary_key=True)
    userinfo_id = db.Column(db.Integer, db.ForeignKey('userinfo.id'))
    userinfo = db.relationship('Userinfo')
    room_id = db.Column(db.Integer, db.ForeignKey('room.id'))
    room = db.relationship('Room')
    status = db.Column(db.Boolean)
    time = db.Column(db.DateTime)

    def __init__(self, userinfo, room, status = True):
        self.userinfo = userinfo
        self.room = room
        self.status = status
        self.time = datetime.datetime.now()